package juegoPlayer;

import java.io.File;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;

public class Sonidos  extends Thread{
	
	private File file;
	private boolean loop;
	private Clip clip;
	
	public Sonidos(String archivo, boolean loop){
		file=new File(archivo);
		this.loop=loop;
	}
	
	public Sonidos(File archivo, boolean loop){
		file=archivo;
		this.loop=loop;
	}
	
	public void run(){
		try
	    {
	        clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class));

	        clip.addLineListener(new LineListener()
	        {
	            @Override
	            public void update(LineEvent event)
	            {
	                if (event.getType() == LineEvent.Type.STOP)
	                    clip.close();
	            }
	        });

	        clip.open(AudioSystem.getAudioInputStream(file));
	        if(loop){
		        clip.loop(Clip.LOOP_CONTINUOUSLY);	        	
	        }else{
	        	clip.start();
	        }
	    }
	    catch (Exception exc)
	    {
	        exc.printStackTrace(System.out);
	    }
	}
	
	public void parar(){
		clip.stop();
	}

}
